﻿package {
	
	import demo.TiledMapSample;
	import demo.UIDemor;
	
	import laya.display.Animation;
	import laya.display.Sprite;
	import laya.display.Stage;
	import laya.display.Text;
	import laya.events.Event;
	import laya.filters.BlurFilter;
	import laya.filters.GlowFilter;
	import laya.net.Loader;
	import laya.resource.Texture;
	import laya.utils.Ease;
	import laya.utils.Handler;
	import laya.utils.Stat;
	import laya.utils.TimeLine;
	import laya.webgl.WebGL;

	public class LayaSample {
		
		//http://bbs.9ria.com/thread-437379-1-1.html 成功配置FB开发环境
		/*
		 1.注意 默认情况下,运行会出现无法加载资源的问题.是因为chrome默认不能跨域加载
			解决:添加启动参数
			在chrome浏览器快捷方式->右击打开属性->快捷方式一栏->目标末尾添加 :--allow-file-access-frome-files
		 2.在layaAir添加资源，需要执行 到处->清理并导出 才能打包新增的资源
		*/
		
		private var _logText:Text;
		private var _redBall:Sprite;
		private var img:Sprite;
		
		public function LayaSample() {
			//初始化引擎
			WebGL.enable();
			Laya.init(1024, 640,WebGL);
			Laya.stage.frameRate = Stage.FRAME_MOUSE;
			Stat.show();
			//DebugTool.init();
			//new HttpReqDemo();
			new WebSocketDemo();
			return;
			new LocalStorageDemo();
			Laya.stage.bgColor = '#cccccc';
			Laya.stage.alignH = "center";
			Laya.stage.alignV = 'middle';
			var res:Array = [];
			for (var i:int = 0; i < 8; i++) 
			{
				res.push({url:"aniImgs/"+(10080+i+1)+".png",type:Loader.IMAGE});
			}
			res.push({url: "res/atlas/comp.json", type: Loader.ATLAS});
			Laya.loader.load(res, Handler.create(this, onLoaded));
			
		}		
		
		private function onLoaded():void
		{
			/*var map:TiledMapSample = new TiledMapSample();
			Laya.stage.addChild(map);
			return;*/
			
			var uit:UIDemor = new UIDemor();
			uit.pos(100,100);
			Laya.stage.addChild(uit);
			return;
			_logText = new Text();
			Laya.stage.addChild(_logText);
			
			_redBall = new Sprite();
			_redBall.graphics.drawCircle(0,0,15,"#ff0000");
			Laya.stage.addChild(_redBall);
			
			Laya.stage.on(Event.MOUSE_MOVE,this,onStageMouseMove);
		}		
		private function testUI():void
		{
			var txt:Text=new Text();
			txt.text = "Hello Laya";
			txt.fontSize = 28;
			txt.color = '#ffffff';
			txt.stroke = 1;
			txt.strokeColor = '#fff000';
			txt.pos(500,50);
			
			var sp1:Sprite = new Sprite();
			sp1.graphics.drawRect(0,0,100,100,'#ff0000');
			sp1.pivot(10,10);
			
			Laya.stage.addChild(txt);
			Laya.stage.addChild(sp1);
			var texture:Texture;
			texture = Loader.getRes("comp/10001.png");
			img = new Sprite();
			img.loadImage("comp/image.png");
			//img.graphics.drawTexture(texture);
			
			img.pos(0,0);
			var glowFl:GlowFilter = new GlowFilter("#fff000");
			var blurFl:BlurFilter = new BlurFilter();
			//img.filters = [glowFl,blurFl]; //设置滤镜 ，必须先实例化然后把实例放进数组
			img.mouseThrough = true; //空白部分不响应鼠标
			//img.graphics.clear();//清空之前的图片
			//img.loadImage() //可以重新load
			//img.graphics.drawTexture();//可以重新draw	
			//img.rotation = -45;
			Laya.stage.addChild(img);
			img.on(Event.CLICK,this,function ():void{
				console.log('clickMe..');
				//Tween.to(img,{x:img.x+500,y:img.y+150},500,Ease.strongIn);
				TimeLine.to(img,{x:img.x+500,y:img.y+150},500,Ease.strongIn)
				.to(img,{x:0,y:0},500,Ease.strongIn)
				.to(img,{x:0,y:200},500,Ease.strongIn).play();
			});
			
			var animation1:Animation;
			animation1 = new Animation();
			animation1.autoPlay = true;
			var frames:String = "";
			for (var i:int = 0; i < 8; i++) 
			{
				frames+='aniImgs/'+(10080+i+1)+".png";
				if(i<7)
				{
					frames+=',';
				}
				
			}
			console.log(frames);
			//通过图片名设置数据
			animation1.source = frames; //ani文件里面的配置图片需要预加载
			animation1.interval = 125;
			animation1.pos(0, 0);
			
			Laya.stage.addChild(animation1);
			//Laya.stage.addChild(new TUIView());
			
			//ProgressBar
		}
		private function onStageMouseMove():void
		{
			if(img && img.hitTestPoint(Laya.stage.mouseX,Laya.stage.mouseY))
			{
				_logText.text = "Hit Me";
			}else
			{
				_logText.text = "Mouse:x,"+Laya.stage.mouseX+",y,"+Laya.stage.mouseY;
			}
			_redBall.pos(Laya.stage.mouseX,Laya.stage.mouseY);
			
		}
	}
}